home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: Compare Function
- Date: 12 Apr 1996 07:04:12 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4kkv9c$6q4@sparcserver.lrz-muenchen.de>
- References: <4kk5jv$41o@freenet-news.carleton.ca>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- aq436@FreeNet.Carleton.CA (Jerry Boyd) writes:
-
- >
- >
- >How would I write a compare function (compare_strings, for
- >instance) that uses character pointers.
- >
- >This is how the compare_strings function should look, but need to
- >implement using pointers.
- >
- >int compare_strings (char s1[], char s2[])
- int compare_strings(char *s1, char *s2)
- >{
- > int i = 0, answer
- int i = 0, answer;
- >
- > while ( s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0' )
- while(*(s1 + i) == *(s2 + i) && *(s1 + i) != '\0' && *(s2 + i) != '\0')
-
- > ++i;
- >
- > if ( s1[i] < s2[i] )
- if (*(s1 + i) < *(s2 + i))
- > answer = -1;
- > else if ( s1[i] == s2[i] )
- else if (*(s1 + i) == *(s2 + i))
- > answer = 0;
- > else
- > answer = 1; /* s1 > s2 */
- >
- > return (answer);
- >}
- >
-
- Because "foo[bar]" is just a different notation for "*((foo) + (bar))",
- your program already uses pointers, so maybe what you want to use are
- '*' characters?
-
- BTW, there is a function called strcmp() in the standard C library
- that might meet your specification quite well.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
-